package cn.ucox.web.ms.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.io.File;
import java.util.Calendar;

/**
 * 调度任务配置
 *
 * @author huqp
 * @create 2017-03-15 10:43
 * @email huqp@oceansoft.com.cn
 */
@Configuration
@Import(DataExchangeConfig.class)
public class JobConfig {

    private static final Logger logger = LoggerFactory.getLogger(JobConfig.class);

    @Resource
    private DataExchangeConfig.ExchangeConfig exchangeConfig;

    @Resource
    private JdbcTemplate pollingJdbcTemplate;

    @Resource
    private DataExchangeConfig.PollingConfig pollingConfig;

    @Scheduled(cron = "0 0 2 * * ?")
//    @Scheduled(cron = "*/10 * * * * *")
    public void deleteBackupFiles() throws Exception {
        logger.debug("读取backup目录文件");
        File file = new File(exchangeConfig.getReceiveBackup());
        File[] files = file.listFiles();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -3);
        long time = calendar.getTimeInMillis();
        logger.debug("" + calendar.getTimeInMillis());
        for (File demoFile : files) {
            demoFile.lastModified();
            if (time > demoFile.lastModified()) {
                demoFile.delete();
            }
        }
        logger.debug("删除五天前{}文件成功",exchangeConfig.getReceiveBackup());
    }

    @Scheduled(cron = "0 0 3 * * ?")
//    @Scheduled(cron = "*/10 * * * * *")
    public void deleteSendFiles() throws Exception {
        logger.debug("读取send目录文件");
        File file = new File(exchangeConfig.getSendBackup());
        File[] files = file.listFiles();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -3);
        long time = calendar.getTimeInMillis();
        logger.debug("" + calendar.getTimeInMillis());
        for (File demoFile : files) {
            demoFile.lastModified();
            if (time > demoFile.lastModified()) {
                demoFile.delete();
            }
        }
        logger.debug("删除五天前{}文件成功",exchangeConfig.getSendBackup());
    }

//    @Scheduled(cron = "0 0 4 * * ?")
////    @Scheduled(cron = "*/10 * * * * *")
//    public void deleteDexSync() throws Exception {
//        logger.debug("删除五天前os_dex_sync表触发记录");
//        String dbType = pollingConfig.getDbType();
//        int result = 0;
//        if(dbType.equalsIgnoreCase("postgresql")){
//            String sql = "delete from os_dex_sync where ctm < (LOCALTIMESTAMP - interval '3 day')";
//            result = pollingJdbcTemplate.update(sql);
//        }else if(dbType.equalsIgnoreCase("oracle")){
//            String sql = "delete from os_dex_sync where ctm < (LOCALTIMESTAMP - 3)";
//            result = pollingJdbcTemplate.update(sql);
//        }
//
//        logger.debug("删除{}数据库五天前os_dex_sync表触发记录{}条成功",dbType,result);
//
//        logger.debug("删除三天前OS_DEX_MEDIUM_SYNC表触发记录");
//        if(dbType.equalsIgnoreCase("postgresql")){
//            String sql = "delete from os_dex_medium_sync where ctm < (LOCALTIMESTAMP - interval '3 day')";
//            result = pollingJdbcTemplate.update(sql);
//        }else if(dbType.equalsIgnoreCase("oracle")){
//            String sql = "delete from os_dex_medium_sync where ctm < (LOCALTIMESTAMP - 3)";
//            result = pollingJdbcTemplate.update(sql);
//        }
//        logger.debug("删除{}数据库三天前OS_DEX_MEDIUM_SYNC表触发记录{}条成功",dbType,result);
//
//        logger.debug("删除三天前OS_DEX_LOW_SYNC表触发记录");
//        if(dbType.equalsIgnoreCase("postgresql")){
//            String sql = "delete from os_dex_low_sync where ctm < (LOCALTIMESTAMP - interval '3 day')";
//            result = pollingJdbcTemplate.update(sql);
//        }else if(dbType.equalsIgnoreCase("oracle")){
//            String sql = "delete from os_dex_low_sync where ctm < (LOCALTIMESTAMP - 3)";
//            result = pollingJdbcTemplate.update(sql);
//        }
//        logger.debug("删除{}数据库三天前OS_DEX_LOW_SYNC表触发记录{}条成功",dbType,result);
//    }
}
